package android.bluebox.view;

import java.util.ArrayList;

import android.app.Activity;
import android.bluebox.R;
import android.bluebox.model.StaticBox;
import android.bluebox.model.Workspace;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AValueWorkspaceListView extends Activity {

	ListView lvWorkspace;
	ArrayList<Workspace> wsListFull = new ArrayList<Workspace>(); 
	ArrayList<String> wsListName = new ArrayList<String>();
	private int aValueId;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.avalueworkspacelist);

		aValueId = this.getIntent().getExtras().getInt("aValueId");
		lvWorkspace = (ListView) findViewById(R.id.avw_list);
		lvWorkspace.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		loadWorkspace();
		lvWorkspace.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, 
				(String[])wsListName.toArray(new String[0])));// convert ArrayList to string[] http://appfulcrum.com/?p=281
		loadCheckedItem();
	}

	protected void onPause() {
		super.onPause();
		SparseBooleanArray checked = lvWorkspace.getCheckedItemPositions();
		for (int i = 0; i < checked.size(); i++) {
			int wsId = wsListFull.get(i).getId();
			if (checked.get(i)) {
				if (StaticBox.dbBox.getCountWorkspaceAValue(wsId, aValueId) < 0)
					StaticBox.dbBox.insertWorkspaceAValue(wsId, aValueId);
			} else {
				StaticBox.dbBox.deleteWorkspaceAValue(wsId, aValueId);
			}
		}
	}

	public void loadWorkspace() {
		StaticBox.dbBox.setContext(this);
		wsListFull = StaticBox.dbBox.getAllWorkspaces();
		for (Workspace workspace : wsListFull) {
			wsListName.add(workspace.getName());
		}
	}

	public void loadCheckedItem() {
		ArrayList<Workspace> list = StaticBox.dbBox.getAValueWorkspaceList(aValueId);
		for (Workspace ws : list) {
			int i = wsListName.indexOf(ws.getName());
			lvWorkspace.setItemChecked(i, true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.avalueworkspacelist_optionsmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.avwl_new_ws:
			Intent intent = new Intent(AValueWorkspaceListView.this, WorkspaceDetailView.class);
			intent.putExtra("wsId", 0);
			startActivityForResult(intent, RESULT_OK);
			return true;

		default:
			return false;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			loadWorkspace();
			lvWorkspace.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, 
					(String[])wsListName.toArray(new String[0])));// convert ArrayList to string[] http://appfulcrum.com/?p=281
			loadCheckedItem();
		}
	}
}
